package ga.wavaplayer.ui {

	import flash.events.Event;
	import ga.wavaplayer.ui.WControls;
	import ga.wemanager.WEManager;
	import ga.wavaplayer.events.WEvent;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
		
	public class WVolumeBar extends Sprite {
		
		private var _config:Object;
		private var _colors:Object;
		
		private var _mouseStatus:Boolean;

		private var _lineColor:uint;
		private var _barColor:uint;
		
		private var _barContainer:Shape = new Shape();
		private var _bar:Shape = new Shape();
		
		private var _volume:Number;
		
		public function WVolumeBar(pConfig:Object, pColors:Object, pVolume:Number) {
			_mouseStatus = false;
			
			_config = pConfig;
			_colors = pColors;
			
			_volume = pVolume;
			
			_lineColor = _colors.semilight;
			_barColor = _colors.medium;
			
			this.buttonMode = true;
			
			this.graphics.beginFill(0xff0000, 0);
			this.graphics.drawRect(0, 0, _config.height * 3, _config.height);
			this.graphics.endFill();
			
			_barContainer.y = Math.floor(_config.height / 3);
			
			this._drawLine();
			this._drawBar();
			
			this.addEventListener(MouseEvent.MOUSE_OVER, _mouseOverHandler);
			this.addEventListener(MouseEvent.MOUSE_OUT, _mouseOutHandler);
			this.addEventListener(MouseEvent.CLICK, _clickHandler);
			this.addEventListener(MouseEvent.MOUSE_DOWN, _mouseDownHandler);
			this.addEventListener(MouseEvent.MOUSE_UP, _mouseUpHandler);
			this.addEventListener(MouseEvent.MOUSE_MOVE, _mouseMoveHandler);
			
			_bar.x = _barContainer.x + 1;
			_bar.y = _barContainer.y + 1;
			
			this.addChild(_barContainer);
			this.addChild(_bar);
			
			updateVolume(_volume);
		}
		
		private function _drawLine():void {
			_barContainer.graphics.clear();
			_barContainer.graphics.beginFill(_lineColor);
			_barContainer.graphics.drawRect(0, 0, _config.height * 3, _config.height - (_barContainer.y * 2));
			_barContainer.graphics.beginFill(_colors.dark);
			_barContainer.graphics.drawRect(1, 1, _config.height * 3 - 2, _config.height - (_barContainer.y * 2) - 2);
			_barContainer.graphics.endFill();
		}
		
		private function _drawBar():void {
			_bar.graphics.clear();
			_bar.graphics.beginFill(_barColor);
			_bar.graphics.drawRect(0, 0, Math.round((_barContainer.width - 2) * _volume), _barContainer.height - 2);
			_bar.graphics.endFill();
		}
		
		private function _mouseOverHandler(e:MouseEvent):void {
			_lineColor = _colors.light;
			_barColor = _colors.semilight;
			this._drawLine();
			this._drawBar();
		}
		
		private function _mouseOutHandler(e:MouseEvent):void {
			_mouseStatus = false;
			_lineColor = _colors.semilight;
			_barColor = _colors.medium;
			this._drawLine();
			this._drawBar();
		}
		
		private function _mouseDownHandler(e:MouseEvent):void {
			_mouseStatus = true;
		}
		
		private function _mouseUpHandler(e:MouseEvent):void {
			_mouseStatus = false;
		}
		
		private function _mouseMoveHandler(e:MouseEvent):void {
			if (_mouseStatus) {
				updateVolume((e.target.mouseX - 1) / (e.target.width - 2));
			}
		}
		
		private function _clickHandler(e:MouseEvent):void {
			updateVolume((e.target.mouseX - 1) / (e.target.width - 2));
		}
		
		private function updateVolume(value:Number):void {
			if(value <= 1){
				_volume = value;
				_drawBar();
				var event:WEvent = new WEvent(WControls.VOLUME_UPDATED);
				event.volume = _volume;
				WEManager.dispatchEvent(event);
			}
		}
	}
}
